package com.brillilab.service.core.hardware.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.hardware.LockOperationMapper;
import com.brillilab.domain.po.hardware.LockOperation;
import com.brillilab.service.constant.JobConstants;
import com.brillilab.service.core.hardware.ILockOperationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
@Transactional
public class ILockOperationServiceImpl implements ILockOperationService {

    @Resource
    private LockOperationMapper lockOperationMapper;

    @Override
    public boolean insert(LockOperation lockOperation) {
        return lockOperationMapper.insert(lockOperation) > 0;
    }

    @Override
    public boolean update(LockOperation lockOperation) {
        lockOperation.setUpdateTime(new Date());
        return lockOperationMapper.updateById(lockOperation) > 0;
    }

    @Override
    public LockOperation selectUsageOperation(Long deviceId,Long labMemberId,Integer operate) {
        return lockOperationMapper.selectOne(
                new LambdaQueryWrapper<LockOperation>()
                        .eq(LockOperation::getDeviceId,deviceId)
                        .eq(LockOperation::getLabMemberId,labMemberId)
                        .eq(LockOperation::getOperate,operate));
    }

    @Override
    public LockOperation selectById(Long operateId) {
        return lockOperationMapper.selectById(operateId);
    }

    @Override
    public boolean changeUnlockToLock(Long deviceId,Long labId,Long labMemberId) {
        return lockOperationMapper.changeUnlockToLock(deviceId,labId,labMemberId) >0;
    }

    @Override
    public LockOperation selectDeleteOperation(Long deviceId,Long labMemberId,Integer operate) {
        return lockOperationMapper.selectOne(
                new LambdaQueryWrapper<LockOperation>()
                        .eq(LockOperation::getDeviceId,deviceId)
                        .eq(LockOperation::getLabMemberId,labMemberId)
                        .eq(LockOperation::getOperate,operate).eq(LockOperation::getJobGroup,JobConstants.JOB_GROUP_DELETE_UNLOCK));
    }
}
